package com.authsphere.security.listener.configuration;

import com.authsphere.security.account.api.lock.AccountLockoutService;
import com.authsphere.security.listener.authentication.failure.credential.BadCredentialsApplicationListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/25 22:07
 **/
@Configuration
public class SecurityListenerConfiguration {

    /**
     * 密码错误异常监听器
     *
     * @return
     */
    @Bean
    @ConditionalOnClass(AccountLockoutService.class)
    @ConditionalOnProperty(prefix = "authsphere.security.listener.authentication.failure", name = "credentials", havingValue = "true")
    public ApplicationListener<AuthenticationFailureBadCredentialsEvent> badCredentialsApplicationListener(final AccountLockoutService accountLockoutService) {
        return new BadCredentialsApplicationListener(accountLockoutService);
    }
}
